package com.ricohgag.controller.admin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ricohgag.entity.VipUser;
import com.ricohgag.service.VipUserService;
import com.ricohgag.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 肖晖
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/admin/vipUser")
public class VipUserController {
    @Autowired
    private VipUserService vipUserService;

    @RequestMapping("/index")
    public ModelAndView toShopAdIndex(@RequestParam(value = "username" , required = false) String name, String startDate, String endDate, @RequestParam(value = "pageNow", required = false) Integer current, HttpServletRequest request) throws Exception{
        System.err.println("start: "+startDate);
        ModelAndView mav = new ModelAndView("/admin/vipUser/index");
        Pager<VipUser> pager = new Pager<>();

        String url = request.getContextPath()+"/admin/vipUser/index";
        if(current == null){
            current = 1;
        }
        Page<VipUser> page = new Page<>(current, 10);

        IPage<VipUser> vipUserPage = vipUserService.selectVipUserPage(name, page, startDate, endDate);

        System.err.println("name: "+name);
        mav.addObject("name",name);
        mav.addObject("Pager", vipUserPage);
        mav.addObject("pages", vipUserPage.getPages());
        mav.addObject("url",url);
        return mav;
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd(Integer id) throws Exception{
        ModelAndView mav = new ModelAndView("admin/vipUser/saveOrUpdate");
        if(!"0".equals(id)){
            VipUser vipUser = vipUserService.selectVipUserOne(id);
            mav.addObject("vipUser", vipUser);
        }
        return mav;
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(VipUser vipUser, HttpServletRequest request) throws Exception{

        //判断id为空则增加，反之则更改
        if(vipUser.getId() == null){
            vipUserService.insertVipUser(vipUser);
        } else {
            vipUserService.updateVipUser(vipUser);
        }

        return "/common/public/reload";
    }

    @RequestMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteShopAd(Integer id, HttpServletResponse response) throws Exception{
        Map<String, String> map = new HashMap<>();
        int count = vipUserService.deleteVipUser(id);

        if(count > 0){
            map.put("msg", "删除成功");
        } else {
            map.put("msg", "删除失败");
        }
        String jsonStr = JSON.toJSONString(map);
        return jsonStr;
    }
}

